package trabajoISW2.domain.implementations;

import java.util.LinkedList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;

import trabajoISW2.domain.contracts.IPublisher;
import trabajoISW2.domain.exceptions.WrongIdException;
import trabajoISW2.domain.exceptions.WrongNameException;

@Entity
public class Publisher implements IPublisher{

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private int publisherId;
	private String name;
	
	@OneToMany (mappedBy = "publisher", cascade = {CascadeType.MERGE, CascadeType.REFRESH})
	private List<Book> books = new LinkedList<Book>();
	
	public Publisher (int publisherId,String name,List<Book> books){
		this.publisherId = publisherId;
		this.name =name;
		this.books = books;
	}
	public Publisher(){
	}
	
	public Publisher(String name2, List<Book> books2) {
		this.name = name2;
		this.books = books2;
	}
	@Override
	public int getPublisherId() {
		return publisherId;
	}
	
	@Override
	public void setPublisherId(int publisherId) {
		if(publisherId<0|publisherId>Integer.MAX_VALUE)
			throw new WrongIdException();
		else
			this.publisherId = publisherId;
	}
	
	@Override
	public String getName() {
		return name;
	}
	
	@Override
	public void setName(String name) {
		if(name == null | name =="")
			throw new WrongNameException();
		else
			this.name = name;
	}
	
	@Override
	public List<Book> getBooks() {
		return books;
	}
	
	@Override
	public void setBooks(List<Book> books) {
		if(books == null)
			throw new IllegalArgumentException();
		else
			this.books = books;
	}

	@Override
	public void addBook(Book book) {
		if(book == null)
			throw new IllegalArgumentException();
		else
			this.books.add(book);
		
	}

	@Override
	public void removeBook(Book book) {
		if(book == null)
			throw new IllegalArgumentException();
		else
			this.books.remove(book);
	}
	
	public boolean equals (Object o){
		boolean b = false;
		Publisher p = (Publisher) o;
		if (getPublisherId() == p.getPublisherId())
			b = true;
		return b;
	}
	



}
